package com.sky.controller.user;

import com.hxc.pay.NativePayTemplate;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.user.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description OrderController
 * @Author lyh
 * @Date 2024-10-09
 */
@RestController
@RequestMapping("/user/order")
@Api(tags = "订单接口管理")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private NativePayTemplate nativePayTemplate;
    @GetMapping("/getEstimatedDeliveryTime")
    @ApiOperation("预计到达时间")
    public Result<String> getEstimatedDeliveryTime(Long shopId,String customerAddress){
        String time=orderService.getEstimatedDeliveryTime(shopId,customerAddress);
        return Result.success(time);
    }

    /**
     * 用户下单
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
   public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO dto){
        OrderSubmitVO orderSubmitVO=  orderService.sumbit(dto);
        return Result.success(orderSubmitVO);
   }

   @PutMapping("/payment")
   @ApiOperation("小程序支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO dto){
       OrderPaymentVO orderPaymentVO=orderService.payment(dto);
       return Result.success(orderPaymentVO);
   }

    @GetMapping("/nativePay")
    @ApiOperation("扫码支付")
    public Result<String> nativePay(String outTradeNo){
        String payUrl = nativePayTemplate.createOrder(0, "苍穹外卖", outTradeNo);
        return Result.success(payUrl);
    }
    @GetMapping("/reminder/{orderId}")
    @ApiOperation("客户催单")
    public Result<String> remainder(@PathVariable Long orderId){
        orderService.remainder(orderId);
        return Result.success();
    }
    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result<PageResult> page(int page, int pageSize, Integer status) {
        PageResult pageResult=orderService.page(page,pageSize,status);
        return Result.success(pageResult);
    }
    @GetMapping("/queryOrdersCheckStatus")
    @ApiOperation("查询可退款的订单")
    public Result<PageResult> queryOrdersCheckStatus(int page, int pageSize, Integer payStatus) {
        PageResult pageResult=orderService.page(page,pageSize,payStatus);
        return Result.success(pageResult);
    }
    @GetMapping("/orderDetail/{orderId}")
    @ApiOperation("获取订单详情")
    public Result<OrderVO> getOrderDetatl(@PathVariable Long orderId){
        OrderVO orderVO=orderService.getOrderDetatl(orderId);
        return Result.success(orderVO);
    }
    @GetMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result<String> cancel(@PathVariable Long id){
        orderService.cancel(id);
        return Result.success();
    }
   @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetition(@PathVariable Long id){
        orderService.repetition(id);
        return Result.success();
   }

}
